<?php
namespace app\api\controller;

use think\Request;

class Huijiao extends Base
{   
    /**
     * @公共方法    获取用户头像信息
     */
    public function username($user_id)
    {
        $res = db('user')->where(['id'=>$user_id])->field('avatarurl')->find();
        
        return $res;
    }
    
    /**
     * @公共方法    获取登入用户学校ID
     */
    public function ShcoolId($tel)
    {
        $res = db('huijiao_zhiwei')->where(['tel'=>$tel])->field('school_id')->find();
    
        return $res;
    }
    
    ##查看学校佣金设置
    public function Commission_type($school_id)
    {
        $res = db('huijiao_extension')->where(['school_id'=>$school_id])->find();
        
        return $res;
    }
    
	/**
	 * 学校接口
	 * **/
	##学校分享/首次登入
	##id=学校ID,longitude=经度,latitude=纬度
	public function School()
	{      
		$post = request()->only(['id','longitude','latitude'],'post');
		
		//分享ID
		if(!empty($post['id'])){
			$where['id'] = $post['id'];
		}
		
		if(!empty($post['longitude']) AND !empty($post['latitude'])){
			//经纬度获取城市区
			$area = "天心区";
			if(!empty($area)){
			$where['area'] = $area;
			}
		}
		
		$where = [];
		//获取学校信息
		$field = 'id,img,name,address,area,content,tel,relation_id,longitude,latitude';
		$res = db('huijiao_school')->field($field)->where($where)->find();
		$res['img'] = json_decode($res['img']);
		foreach ($res['img'] as $k => $v){
		    $res['img'][$k] = $this->game_img($v);
		}
		//附近校区
		$where = [];
		$field = [];
		$field = 'id,image,name,area,address,longitude,latitude,tel';
		$where['relation_id'] = $res['id'];
		$where['id'] = ['neq',$res['id']];
		$res['campus'] = db('huijiao_school')->field($field)->where($where)->select();
		
		foreach ($res['campus'] as $k => $v){
		    if (!empty($v['image'])){
		        $res['campus'][$k]['image'] = $this->game_img($v['image']);
		    }
		    
		}


		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
		
		
	}
	
	##获取课程列表
	public function curriculum()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		$field = 'id,name,img,content,original_price,price';
		$res = db('huijiao_curriculum')->field($field)->where(['school_id'=>$post['school_id']])->select();
		
		foreach ($res as $k =>$v){
		    $res[$k]['img'] = $this->game_img($v['img']);
		}
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	##获取名师列表
	public function teacher()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		$field = 'id,name,img,title';
		$res = db('huijiao_teacher')->field($field)->where(['school_id'=>$post['school_id']])->select();
		foreach ($res as $k =>$v){
		    $res[$k]['img'] = $this->game_img($v['img']);
		}
		
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	##获取文章列表
	public function article()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		$field = 'id,introduction,img,title';
		$res = db('huijiao_article')->field($field)->where(['school_id'=>$post['school_id']])->select();
		
		foreach ($res as $k =>$v){
		    $res[$k]['img'] = $this->game_img($v['img']);
		}
		
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	##文章详情
	public function article_cat()
	{
		$post = request()->only(['id'],'post');
		
		if(empty($post['id'])){
			err('文章ID不能为空');
		}
		
		//增加阅读量
		db('huijiao_article')->where(['id'=>$post['id']])->setInc('reading_volume');
		
		$field = 'content,title,create_time,name,reading_volume,mv,type';
		$res = db('huijiao_article')->field($field)->where(['id'=>$post['id']])->find();
		$res['mv'] = $this->game_img($res['mv']);
		
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	##学校咨询提交
	public function Consultation()
	{
		$post = request()->only(['school_id','name','tel'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		if(empty($post['name'])){
			err('姓名不能为空');
		}
		
		if(empty($post['tel'])){
			err('电话不能为空');
		}
		
		$info = db('huijiao_consultation')
		->where(['name'=>$post['name'],'user_id'=>$this->accessData['uid']])
				->find();

		if($info){
			err('请勿重复提交');
		}
		
		$post['admin_id'] = $this->accessData['aid'];
		$post['user_id'] = $this->accessData['uid'];
		$post['create_time'] = date("Y-m-d H:i:s");
		$res = db('huijiao_consultation')->insert($post);
		
		if(!$res){
			err('咨询失败');
		}else{
			suc('咨询成功');
		}
	}
	
	##学校(机构)申请提交
	public function Apply()
	{
		$post = request()->only(['name','tel','type'],'post');
		
		if(empty($post['tel'])){
			err('电话不能为空');
		}
		
		if(empty($post['name'])){
			err('名称不能为空');
		}
		
		if(empty($post['type'])){
			err('类型不能为空');
		}
		
		$info = db('huijiao_school_apply')->where(['name'=>$post['name']])
				->find();
				
		if($info){
			err('请勿重复提交');
		}
		
		$post['admin_id'] = $this->accessData['aid'];
		$post['user_id'] = $this->accessData['uid'];
		$post['create_time'] = date("Y-m-d H:i:s");
		
		$res = db('huijiao_school_apply')->insert($post);
		
		if(!$res){
			err('申请失败');
		}else{
			suc('申请成功');
		}
		
	}
	
	##机构类型
	public function Type()
	{
		$res = db('huijiao_type')->field('id,name')->select();	
		
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	
	/**
	 * 推广设置
	 * **/
	
	##推广设置展示页面
	public function Extension()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		$field = 'read_money,sign_money,recommend_money,free_read_money,free_sign_money,state,distance,purchase';
		$res = db('huijiao_extension')
				->where(['school_id'=>$post['school_id']])
				->field($field)
				->find();
		
		if(!$res){
			err('',$res);
		}	

		suc('',$res);

	}
	
	##编辑佣金
	public function Commission()
	{
		$post = request()->only(['school_id','type','money'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		if(empty($post['type'])){
			err('佣金类型不能为空');
		}
		
		if(empty($post['money'])){
			$post['money'] = 0;
		}
		
		$info = db('huijiao_extension')
				->where(['school_id'=>$post['school_id']])
				->find();
		
		$data['admin_id'] = $this->accessData['aid'];
		$data[$post['type']] = $post['money'];		
		if($info){
			$res = db('huijiao_extension')
				   ->where(['school_id'=>$post['school_id']])
				   ->update($data);
		}else{
			$data['school_id'] = $post['school_id'];
			$res = db('huijiao_extension')->insert($data);
		}
		
		if(!$res){
			err('设置失败');
		}else{
			suc('设置成功');
		}
	}
	
	
	##推广地图
	public function Map()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		$res = db('huijiao_school')->field('longitude,latitude')
				->where(['id'=>$post['school_id']])
				->find();
				
		if(!$res){
			err('暂无数据');
		}else{
			suc('',$res);
		}
	}
	
	##推广查询
	public function Extension_query()
	{
		$post = request()->only(['school_id'],'post');
		
		if(empty($post['school_id'])){
			err('学校ID不能为空');
		}
		
		//余额、课程收入
		$res = db('huijiao_extension')
				->where(['school_id'=>$post['school_id']])
				->field('balance,income')
				->find();
				
		//今日消费
		$res['day_money'] = 200;
		
		//线上报名缴费
		$res['curriculum'] = [];
		$res['curriculum'] = db('huijiao_curriculum_order')
							->where(['school_id'=>$post['school_id'],'type'=>1])
							->field('name,tel,curriculum,money,create_time')
							->select();
		
		//线上成交资源
		$res['curriculum_type'] = [];
		$res['curriculum_type'] = db('huijiao_curriculum_order')
							->where(['school_id'=>$post['school_id'],'type'=>2])
							->field('name,tel,curriculum,money,create_time')
							->select();
							
		//推荐生源
		$res['recommend'] = [];
		$res['recommend'] = db('huijiao_recommend')->alias('a')
							->join('huijiao_extension b','a.school_id = b.school_id','left')
							->where(['a.school_id'=>$post['school_id']])
							->field('a.id,a.name,a.recommend_number,a.complaint,a.state,b.recommend_see,a.tel')
							->select();
							
		//咨询生源
		$res['consultation']=[];
		$res['consultation'] = db('huijiao_consultation')->alias('a')
							->join('huijiao_extension b','a.school_id = b.school_id','left')
							->where(['a.school_id'=>$post['school_id']])
							->field('a.id,a.name,a.recommend_number,a.complaint,a.state,b.recommend_see,a.tel')
							->select();
		
		$day = date("Y-m-d");
		//今日被分享阅读次数
		$res['share'] = [];
		$res['share'] = db('huijiao_share')
						->where(['school_id'=>$post['school_id'],'create_time'=>$day])
						->count('id');
		
		suc('',$res);
	}
	
	##投诉类型
	public function Complaint()
	{
	    $res = db('huijiao_complaint')->field('id,name')->select();
	    
	    if ($res){
	        suc('',$res);
	    }else{
	        err('暂无数据');
	    }
	}
	
	##投诉提交
	public function Complaint_post()
	{
	    $post = request()->only(['id','reason'],'post');
	    
	    if (empty($post['id'])){
	        err('ID不能为空');
	    }
	    if (empty($post['reason'])){
	        err('投诉不能为空');
	    }
	    
	    $res = db('huijiao_recommend')->where(['id'=>$post['id']])->update(['complaint'=>$post['reason']]);
	    
	    if ($res){
	        suc('成功');
	    }else{
	        err('失败');
	    }
	}
	
	/**
	 * 校区总代
	 * **/

	##管理员登入
	public function Log_in()
	{
	    $post = request()->only(['tel'], 'post');
	    
	    //手机不能为空
	    if (empty($post['tel'])){
	        err('号码不能为空');
	    }
	    
	    $my_tel = ['15873095321','15873095322','15873095323','15873095324','15873095325','15873095326'];
	    if (!in_array($post['tel'], $my_tel)) {
	        $verifyCode = mt_rand(1000, 9999);
            $return = $this->sendSms($post['tel'],$verifyCode);
            $return = ((array)$return);

            
            if (!empty($return['result'])){
                $return['code'] = 1;
            }else{
                $return['code'] = 0;
            }
	     } else {
	        $verifyCode = 8888;
	        $return = ['code'=>1, 'sub_msg'=>'发送成功'];
	     }

	     // 短信发送失败
	     if ($return['code'] != 1 ) {
	        err($return['sub_msg']);
	     }
	
	     $resData = [];
	     $resData['tel'] = $post['tel'];
	     $resData['verify_code'] = $verifyCode;
	     $resData['time'] = $this->timeStamp;
	     $resData['mid'] = md5($post['tel'] . $verifyCode . $this->timeStamp . $this->signKey);
	
	     suc('success', $resData);
	}
	
	
	##验证短信验证码
	public function verifyNote()
	{
	    $post = request()->only(['tel', 'time', 'mid', 'verify_code']);
	
	    $time_new = time() - $post['time'];
	    if ($post['mid'] != md5($post['tel'] . $post['verify_code'] . $post['time'] . $this->signKey)) {
	        err('验证失败');
	    }elseif ($time_new > 60){
	        err('验证超时');
	    }else{
	        $res = db('huijiao_zhiwei')->where(['admin_id'=>$this->accessData['aid'],'tel'=>$post['tel']])->field('type,school_id')->find();
	        
	        if (!$res){
	            err('暂无权限');
	        }
	        
	        suc('验证成功',$res);
	    }
	
	    
	}
	
	
	##邀请伙伴(学校名师)页面-分享(总代端)
	public function Invitation()
	{
	    $post = request()->only(['school_id','page','pagesize'],'post');
	    
	    if (empty($post['school_id'])){
	        err('school_id not NULL');
	    }
	    
	    if (empty($post['page'])) {
	        $post['page'] = 0;
	    }
	    if (empty($post['pagesize'])) {
	        $post['pagesize'] = 10;
	    }
	    $page = $post['page'];
	    $pagesize = $post['pagesize'];
	    
	    $where['a.admin_id'] = $this->accessData['aid'];
	    $where['a.school_id'] = $post['school_id'];
	    $where['a.statr'] = ['neq',3];
	    
	    $field = "a.id,a.name,a.tel,a.statr,a.create_time,b.avatarurl";
	    $res = db('huijiao_teacher')->alias('a')
	           ->join('user b','a.user_id = b.id','left')
	           ->where($where)->field($field)->page($page, $pagesize)->select();
	    
	    foreach ($res as $k => $v){
	        $res[$k]['day'] = intval((time() - strtotime($v['create_time'])) / 86400).'天前';
	    }
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	}
	
	##修改状态
	public function Invitation_edit()
	{
	    $post = request()->only(['id','statr'],'post');
	     
	    if (empty($post['id'])){
	        err('id not NULL');
	    }
	    
	    if (empty($post['statr'])){
	        err('statr not NULL');
	    }
	    
	    $res = db('huijiao_teacher')->where(['id'=>$post['id']])->update(['statr'=>$post['statr']]);
	    
	    if (!$res){
	        err('失败');
	    }else{
	        suc('成功');
	    }
	}
	
	/**
	 * @校区总代邀请伙伴
	 * @tel    登入入口手机号码 用于判断学校ID
	 */
	public function InvitingPartners()
	{
	    $post = request()->only(['tel'],'post');
	    
	    if (empty($post['tel'])){
	        err('tel not null');
	    }
	    
	    $res = $this->ShcoolId($post['tel']);
	    
	    suc('机构ID',$res);
	    
	}
	
	/**
	 * @点击校区总代邀请分享
	 * @school_id  机构ID   必传参数
	 */
	public function AcceptInvitation()
	{
	    $post = request()->only(['school_id'],'post');
	     
	    if (empty($post['school_id'])){
	        err('school_id not null');
	    }
	    
	    ##查询是否重复提交
	    $info = db('huijiao_teacher')->where(['user_id'=>$this->accessData['uid'],'school_id'=>$post['school_id']])->find();
	    
	    if (!$info){
	        $post['admin_id'] = $this->accessData['aid'];
	        $post['user_id'] = $this->accessData['uid'];
	        $post['school_id'] = $post['school_id'];
	        $post['statr'] = 3;
	        $post['create_time'] = date("Y-m-d");
	        db('huijiao_teacher')->insert($post);
	    }
	    
	    suc('ok');
	}
	
	
	/**
	 * @统计排名
	 * @余额单位(分)    round(x,prec);
	 */
	public function Ranking()
	{
	    $post = request()->only(['school_id','day','page','pagesize'],'post');

	    if (empty($post['school_id'])){
	        err('school_id not NULL');
	    }
	     
	    if (empty($post['page'])) {
	        $post['page'] = 0;
	    }
	    if (empty($post['pagesize'])) {
	        $post['pagesize'] = 10;
	    }
	    
	    if (empty($post['day'])){
	        $day = date('m',time());
	    }else{
	        $day = $post['day'];
	    }

	    $page = $post['page'];
	    $pagesize = $post['pagesize'];
	    
	    $where['a.admin_id'] = $this->accessData['aid'];
	    $where['a.school_id'] = $post['school_id'];
	    $where['a.statr'] = ['neq',3];
	     
	    $field = "a.name,a.balance,b.avatarurl,a.create_time";
	    $res['earnings'] = db('huijiao_teacher')->alias('a')
	    ->join('user b','a.user_id = b.id','left')
	    ->where($where)->field($field)->page($page, $pagesize)->order('balance desc')->select();

	    ##总收益
	    $res['all'] = 0;
	    $res['only'] = 0;
	    foreach ($res['earnings'] as $k => $v){

	        $res['all'] = $res['all'] + $v['balance'];
	        
	            ##格式化，取出月份；
	            $new_day = date('m',strtotime($v['create_time']));
	            
	            if ($day == $new_day){
	                $res['only'] = $res['only'] + $v['balance'];
	            }
	        
	        
	        
	    }
	    
	    //可提现金额
	    $arr = db('huijiao_zhiwei')
	               ->where(['admin_id'=>$this->accessData['aid'],'school_id'=>$post['school_id'],'type'=>2])->field('id,balance')->find();
	    
	    $res['money'] = $arr['balance'];
	    $res['pid'] = $arr['id'];
	    
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	    
	}
	
	
	##提现列表
	public function Withdrawal_list()
	{
	     $post = request()->only(['pid','page','pagesize'],'post');
	     
	    if (empty($post['pid'])){
	        err('pid not NULL');
	    }
	     
	    if (empty($post['page'])) {
	        $post['page'] = 0;
	    }
	    if (empty($post['pagesize'])) {
	        $post['pagesize'] = 10;
	    }
	    $page = $post['page'];
	    $pagesize = $post['pagesize'];
	    
	    $res['withdrawal'] = db('huijiao_withdrawal')
	           ->where(['admin_id'=>$this->accessData['aid'],'pid'=>$post['pid']])
	           ->page($page, $pagesize)->order('create_time desc')->field('money,create_time,statr')->select();
	    
	    $res['momey'] = 0;
	    foreach ($res['withdrawal'] as $k => $v){
	        $res['momey'] = $res['momey'] + $v['money'];
	    }
	    
        if (!$res){
           err('暂无数据');
        }else{
           suc('',$res);
        }
	}
	
	##提现申请
	public function Withdrawal_add()
	{
        $post = request()->only(['pid','account_number','account_name','money'],'post');

	    if (empty($post['pid'])){
	        err('pid not NULL');
	    }
	    
	    if (empty($post['account_name'])){
	        err('account_name not NULL');
	    }
	    
	    if (empty($post['account_number'])){
	        err('account_number not NULL');
	    }
	    
	    if (empty($post['money'])){
	        err('money not NULL');
	    }
	    
	    //判断余额是否还够
	    $arr = db('huijiao_zhiwei')
	    ->where(['admin_id'=>$this->accessData['aid'],'id'=>$post['pid'],'type'=>2])->field('balance')->find();
	    
	    if ($post['money'] > $arr['balance']){
	        err('余额不够');
	    }
	    
	    
	    $data['admin_id'] = $this->accessData['aid'];
	    $data['pid'] = $post['pid'];
	    $data['type'] = 1;
	    $data['account_name'] = $post['account_name'];
	    $data['account_number'] = $post['account_number'];
	    $data['money'] = $post['money'];
	    $data['create_time'] = date("Y-m-d H:i:s");
	    $res = db('huijiao_withdrawal')->insert($data);
	    
	    if (!$res){
	        err('提现失败');
	    }else{
	        //余额-出提现金额
	        $data = [];
	        $data['balance'] = $arr['balance'] - $post['money'];
	        db('huijiao_zhiwei')->where(['id'=>$post['pid']])->update($data);
	        
	        suc('提现成功');
	    }
	}
	
	##学校/老师收益
	public function TeacherProfit()
	{
	    $res = db('huijiao_earnings')->where(['user_id'=>$this->accessData['uid']])->find();

	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	}
	
	
	##文章搜索
	public function article_search()
	{
	    $post = request()->only(['title','school_id'],'post');
	    
	    if (empty($post['school_id'])){
	        err('school_id not NULL');
	    }
	    
	    if (empty($post['title'])){
	        err('请输入搜索内容');
	    }else{
	        $where['title'] = ['like','%'.$post['title'].'%'];
	    }
	    
	    $where['admin_id'] = $this->accessData['aid'];
	    $where['school_id'] = $post['school_id'];
	    $field = "id,title,create_time,introduction,reading_volume,intention_number,img,share_number";
	    $res = db('huijiao_article')->where($where)->field($field)->select();
	    
	    foreach ($res as $k => $v){
	        $res[$k]['img'] = $this->game_img($v['img']);
	    }
	    
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	}
	
    ##老师咨询文章
    public function Teacher_article()
    {
        $post = request()->only(['school_id','type','sort'],'post');
        
        if (empty($post['school_id'])){
            err('school_id not NULL');
        }
        
        if (empty($post['type']) || $post['type'] == 1){
            $type = "create_time";
        }elseif($post['type'] == 2){
            $type = "reading_volume";
        }elseif($post['type']== 3){
            $type = "intention_number";
        }
        
        if (empty($post['sort'])){
            $sort = 'DESC';
        }else{
            $sort = $post['sort'];
        }
        
        $where['admin_id'] = $this->accessData['aid'];
        $where['school_id'] = $post['school_id'];
        $field = "id,title,create_time,introduction,reading_volume,intention_number,img,share_number";
        $res = db('huijiao_article')->where($where)->field($field)->order("$type $sort")->select();
        
        foreach ($res as $k => $v){
            $res[$k]['img'] = $this->game_img($v['img']);
        }

        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
    
	
    ##文章详情以及关键词推荐机构
    public function article_cats()
    {
        $post = request()->only(['id'],'post');
        
        if (empty($post['id'])){
            err('id not NULL');
        }
        
        $res['article'] = db('huijiao_article')->where(['id'=>$post['id']])->field('mv,title,type,content,create_time,name,reading_volume,key_word')->find();

        $res['article']['mv'] = $this->game_img($res['article']['mv']);

        
        $res['school'] = db('huijiao_school')->field('image,name,content')->where(['key_word'=>['like','%'.$res['article']['key_word'].'%']])->select();
        
        foreach ($res['school'] as $k => $v){
            $res['school'][$k]['image'] = $this->game_img($v['image']);
        }
        
        if (!$res){
            err('暂无数据');
        }else{
            suc('',$res);
        }
    }
	
	
	##机构列表
	public function school_list()
	{
	    $post = request()->only([ 'type','state'],'post');
	    
	    if (empty($post['type']) || $post['type'] == 1){
	        $type = "a.sort";
	    }elseif($post['type'] == 2){
	        $type = "a.create_time";
	    }elseif($post['type']== 3){
	        $type = "a.tuition";
	    }elseif($post['type'] == 4){
	        if (empty($post['state'])){
	            err('state not NULL');
	        }
	        
	        $arr = db('huijiao_type')->where(['name'=>$post['state']])->field('id')->find();
	        $where['a.state'] = $arr['id'];
	        $type = "a.id";
	    }
	    
	    $where['a.admin_id'] = $this->accessData['aid'];
        $field = "a.id,a.image,a.name,a.content,a.area,a.tuition,b.read_money,b.sign_money,b.recommend_money,b.purchase";
	    $res = db('huijiao_school')->alias('a')->join('huijiao_extension b','a.id =b.school_id','left')
	           ->where($where)->order("$type desc")->field($field)->select();

	    foreach ($res as $k =>$v){
	        $res[$k]['image'] = $this->game_img($v['image']);
	    }
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	    
	}
	
	##老师端推荐学生
	public function Recommend()
	{
	    $post = request()->only(['school_id','name','tel'],'post');
	    
	    if (empty($post['name'])){
	        err('name not NULL');
	    }
	    
	    if (empty($post['tel'])){
	        err('tel not NULL');
	    }
	    
	    if (empty($post['school_id'])){
	        err('school_id not NULL');
	    }
	    
	    //查询是否被推荐过
	    $arr = db('huijiao_consultation')->where(['name'=>$post['name'],'tel'=>$post['tel']])->find();
	    
	    if ($arr){
	        $res = db('huijiao_consultation')->where(['id'=>$arr['id']])->setInc('recommend_number');
	    }else{
	        $post['admin_id'] = $this->accessData['aid'];
	        $post['user_id'] = $this->accessData['uid'];
	        $post['recommend_number'] = 1;
	        $post['state'] = 0;
	        $post['type'] = 2;
	        $post['create_time'] = date("Y-m-d H:i:s");
	        $res = db('huijiao_consultation')->insert($post);
	    }
	    
	    if (!$res){
	        err('推荐失败');
	    }else{
	        suc('推荐成功');
	    }
	}
	
	
	##分享传播
	public function share()
	{
	    $post = request()->only(['school_id'],'post');
	    //增加分享记录
	    $post['admin_id'] = $this->accessData['aid'];
	    $post['user_id'] = $this->accessData['uid'];
	    $post['create_time'] = date("Y-m-d");
	    $res = db('huijiao_share')->insert($post);
	    
	    $arr['user_id'] = $post['user_id'];
	    $arr['school_id'] = $post['school_id'];
	    
	    if (!$res){
	        err('分享失败');
	    }else{
	        suc('',$arr);
	    }
	}
	
	
	##点击分享内容
	public function click_share()
	{
	    $post = request()->only(['user_id','school_id'],'post');
	    
	    if (empty($post['user_id'])){
	        err('user_id not NULL');
	    }
	    
	    if (empty($post['school_id'])){
	        err('school_id not NULL');
	    }
	    
	    $day = date("Y-m-d");
	    
	    //判断今天是否点开过
	    $arr = db('huijiao_earnings')
	           ->where(['pid'=>$post['user_id'],'user_id'=>$this->accessData['uid'],'school_id'=>$post['school_id'],'create_time'=>$day])
	           ->find();
	    
	    if (!$arr){
	        //成功点开分享+有效查看佣金
	         
	        $momey = $this->Commission_type($post['school_id']);
	         
	        $read_money = $momey['read_money'];
	         
	        //增加老师收益记录
	        $data['admin_id'] = $this->accessData['aid'];
	        $data['user_id'] = $this->accessData['uid'];
	        $data['school_id'] = $post['school_id'];
	        $data['pid'] = $post['user_id'];
	        $data['reading_id'] = $post['school_id'];
	        $data['reading_money'] = $read_money;
	        $data['create_time'] = date("Y-m-d");
	        $res = db('huijiao_earnings')->insert($data);
	        
	        //老师增加余额
	        $list = db('huijiao_zhiwei')->where(['user_id'=>$post['user_id']])->find();
	        $cat = $list['balance'] + $read_money;
	        db('huijiao_zhiwei')->where(['user_id'=>$post['user_id']])->update(['balance'=>$cat]);
	    }
	    
	        suc('OK');

	}
	
	
	public function sendSms($tel,$code)
	{
	    include_once EXTEND_PATH . 'Message/top/TopClient.php';
	    include_once EXTEND_PATH . 'Message/top/request/AlibabaAliqinFcSmsNumSendRequest.php';

	    $c = new \TopClient;
	
	    $c->appkey = "23378400";
	    $c->secretKey = "4fad19a57bf6ede8ef664adbd5dfc944";
	    $req = new \AlibabaAliqinFcSmsNumSendRequest;
	    $req->setExtend($code);
	    $req->setSmsType("normal");
	    $req->setSmsFreeSignName("惠教网");
	    $req->setSmsParam("{\"code\":\"$code\",\"product\":\"alidayu\"}");
	    $req->setRecNum($tel);
	    $req->setSmsTemplateCode("SMS_135355107");
	    $resp = $c->execute($req);
	    return $resp;
	}

	/**
	 * 省/市/区代理
	 * 端口 5
	 * tel:登入的电话号码
	 */
	
	##机构列表
	public function AgentSchoolList()
	{
	    $post = request()->only(['tel','type','state','search'],'post');
	    
	    if (empty($post['tel'])){
	        err('tel not null');
	    }
	    
	    if (empty($post['type']) || $post['type'] == 1){
	        $type = "a.sort";
	    }elseif($post['type'] == 2){
	        $type = "a.create_time";
	    }elseif($post['type']== 3){
	        $type = "a.tuition";
	    }elseif($post['type'] == 4){
	        if (empty($post['state'])){
	            err('state not NULL');
	        }
	         
	        $arr = db('huijiao_type')->where(['name'=>$post['state']])->field('id')->find();
	        $where['a.state'] = $arr['id'];
	        $type = "a.id";
	    }
	    
	    $where['admin_id'] = $this->accessData['aid'];
	    $where['tel'] = $post['tel'];
	    //查询用户信息
	    $user_info = db('huijiao_zhiwei')->where($where)->field('province,type')->find();
	    
	    if ($user_info['type'] == 5){
	        $province = 'a.province';
	    }elseif($user_info['type'] == 7){
	        $province = 'a.city';
	    }elseif($user_info['type'] == 8){
	        $province = 'a.area';
	    }
	    
	    $where = [];
	    $where[$province] = $user_info['province'];
	    
	    if (!empty($post['search'])){
	        $where['name'] = ['like','%'.$post['search'].'%'];    
	    }
	    //学校列表
	    $where['a.admin_id'] = $this->accessData['aid'];
        $field = "a.id,a.image,a.name,a.content,a.area,a.tuition,b.read_money,b.sign_money,b.recommend_money,b.purchase";
	    $res = db('huijiao_school')->alias('a')->join('huijiao_extension b','a.id =b.school_id','left')
	           ->where($where)->order("$type desc")->field($field)->select();

	    foreach ($res as $k =>$v){
	        $res[$k]['image'] = $this->game_img($v['image']);
	    }
	    
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	}
	
	##合作伙伴
	public function Cooperation()
	{
	    $post = request()->only(['tel'],'post');
	    
	    $day = time();
	    if (empty($post['tel'])){
	        err('tel not null');
	    }
	    
	    $where['admin_id'] = $this->accessData['aid'];
	    $where['tel'] = $post['tel'];
	    //查询用户信息
	    $user_info = db('huijiao_zhiwei')->where($where)->field('id,type,province')->find();
	    $where = [];
	    if ($user_info['type'] == 5){
	            $res = db('huijiao_zhiwei')->field('id,user_id,name,tel,statr,create_time')->where(['type'=>7,'province_id'=>$user_info['id']])->select();
    	    }elseif($user_info['type'] == 7){
        	        $res = db('huijiao_zhiwei')->field('id,user_id,name,tel,statr,create_time')->where(['type'=>8,'province_id'=>$user_info['id']])->select();
        	    }elseif($user_info['type'] == 8){
        	        $school = db('huijiao_school')->where(['area'=>$user_info['province']])->field('id')->select();
        	        
        	        $res = [];
        	        foreach ($school as $k => $v){
        	            $where['type'] = 2;
        	            $where['school_id'] = $v['id'];
        	            $res = db('huijiao_zhiwei')->where($where)->field('id,user_id,name,tel,statr,create_time')->select();
        	            
        	        }
    	        }
    	
    	
        foreach ($res as $key => $val){
            $res[$key]['img'] = $this->username($val['user_id']);
            
            $new_day = intval(($day - strtotime($val['create_time'])) / 86400).'天前';
            
            if ($new_day == 0){
                $res[$key]['day'] = "一天内";
            }else{
                $res[$key]['day'] = $new_day;
            }
            
        }
	    
	    if (!$res){
	        err('暂无数据');
	    }else{
	        suc('',$res);
	    }
	}
}








